﻿using System;
using System.Collections.Generic;
using System.Text;
using Antu.WorkFlowFactory;
using Antu.Core.Services;
using System.Web.UI;
using System.Data;
using System.Web.UI.WebControls;
using System.Configuration;

namespace WebSys.UI.ZhengShou.OfficeFlow
{
    public class priority_main:Page
    {
        protected global::Antu.WebControls.CustomGridView gv;
        protected global::Antu.WebControls.MPCalendar start_time;
        protected global::Antu.WebControls.MPCalendar end_time;
        protected global::System.Web.UI.WebControls.TextBox danwei;
        protected global::System.Web.UI.WebControls.Button button1;
        protected global::System.Web.UI.WebControls.DropDownList ddltype;
        FlowFactoryProvider _FlowFactoryProvider = (FlowFactoryProvider)ProviderService.ProviderServiceMange.GetProvider("FlowFactoryProvider");

        protected string UserId
        {
            get { if (base.Page.Session["UserId"] != null) { return base.Page.Session["UserId"].ToString(); } else { return string.Empty; } }
            set { base.Page.Session["UserId"] = value; }
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (!IsPostBack)
            {
                BindDDL();
                BindData();
            }
            button1.Click += new EventHandler(button1_Click);
        }

        void button1_Click(object sender, EventArgs e)
        {
            BindData();
        }

        public void BindData()
        {
            string where = "  and 案件类型 in("+ ConfigurationManager.AppSettings["ZSAnJianType"].ToString()+")";
            if (!string.IsNullOrEmpty(start_time.Text.Trim()) && string.IsNullOrEmpty(end_time.Text.Trim()))
            {
                where += " and 接收时间 >=to_date('" + start_time.Text.Trim() + "','yyyy-MM-dd')";
            } if (!string.IsNullOrEmpty(start_time.Text.Trim()) && !string.IsNullOrEmpty(end_time.Text.Trim()))
            {
                where += " and 接收时间 >=to_date('" + start_time.Text.Trim() + "','yyyy-MM-dd') and 接收时间 <=to_date('" + end_time.Text.Trim() + "','yyyy-MM-dd')";
            }
            if (string.IsNullOrEmpty(start_time.Text.Trim()) && !string.IsNullOrEmpty(end_time.Text.Trim()))
            {
                where += " and 接收时间 <=to_date('" + end_time.Text.Trim() + "','yyyy-MM-dd')";
            }
            if (!string.IsNullOrEmpty(ddltype.SelectedItem.Text.Trim()))
            {
                if (ddltype.SelectedItem.Text.Trim() != "全部")
                {
                    where += " and 案件类型='" + ddltype.SelectedItem.Text.Trim() + "'";
                }
            }
            if (!string.IsNullOrEmpty(danwei.Text.Trim()))
            {
                where += " and 申请单位 like '%" + danwei.Text.Trim() + "%'";
            }
            DataTable dt = _FlowFactoryProvider.GetInstanceListByUserId(UserId, where);
            gv.DataSource = dt;
            gv.RecordCount = dt.Rows.Count;
            gv.DataBind();
        }

        private void BindDDL()
        {
           // DataTable dt = _FlowFactoryProvider.GetUserRightWname(UserId);
            string ajlx = ConfigurationManager.AppSettings["ZSAnJianType"].ToString();
            DataTable dt = _FlowFactoryProvider.NewGetStartWorkFlowListByUserId(UserId, ajlx);
            ListItem item = new ListItem("全部");
            List<ListItem> itemlist = new List<ListItem>();
            itemlist.Add(item);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int temp = 0; temp < dt.Rows.Count; temp++)
                {
                    item = new ListItem(dt.Rows[temp]["wname"].ToString());
                    itemlist.Add(item);
                }
                ddltype.DataSource = itemlist;
                ddltype.DataBind();
            }
        }

        protected void gv_OnLoadData(object sender, EventArgs e)
        {
            BindData();
        }
        public void gv_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                case DataControlRowType.Header:
                    break;
                case DataControlRowType.DataRow:
                    DropDownList ddlyxj = (DropDownList)e.Row.FindControl("ddlyxj");
                    DataRowView drv = (DataRowView)e.Row.DataItem;
                    string siid = drv["siid"].ToString();
                    ddlyxj.SelectedValue = drv["优先级"].ToString();
                    ddlyxj.ToolTip = siid;
                    //ddlyxj.Attributes.Add("onchange","tosubmit('"+ddlyxj.ID+"','"+siid+"')");

                    break;
            }
        }

        public void ddlyxj_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddl = (DropDownList)sender;
            string iid = ddl.ToolTip;
            if (!(string.IsNullOrEmpty(iid) && string.IsNullOrEmpty(ddl.SelectedValue)))
            {
                int count = _FlowFactoryProvider.UpdateInstancePriority(iid, ddl.SelectedValue);
                if (count > -1)
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('设置成功')</script>");
                }
            }

        }
    }
}
